package com.wdf.project03.controller;

import com.wdf.project03.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@Slf4j
public class FileCtroller {
    @PostMapping("/upload")
    private Result upload(MultipartFile image) throws Exception{
        log.info("文件上传：{}",image);
        if (image.isEmpty()){
            return Result.error("上传失败");
        }else {
            int index = image.getOriginalFilename().toString().lastIndexOf(".");
            String extname = image.getOriginalFilename().toString().substring(index);
            String newPhotoName = UUID.randomUUID().toString() + extname;
            image.transferTo(new File("D:\\Java\\nginx-1.22.0-tlias\\html\\static\\img\\" + newPhotoName));
            String imagePath = "\\static\\img\\" + newPhotoName;
            return  Result.success(imagePath);
        }
    }
}
